package com.abu.vodms.service;

import com.abu.exceptionhandler.EduException;
import com.abu.vodms.template.VodTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class VodService {

    @Value("${vod.accessKeyId}")
    private String accessKeyId;

    @Value("${vod.accessKeySecret}")
    private String accessKeySecret;


    public String uploadVideo(MultipartFile file) {

        String filename = file.getOriginalFilename();
        String title = StringUtils.substringBeforeLast(filename, ".");
        String videoId = null;
        try {
            videoId = VodTemplate.uploadStream(accessKeyId, accessKeySecret, title, file.getOriginalFilename(), file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (StringUtils.isBlank(videoId)) {
            throw new EduException(20001, "获取videoId失败");
        }
        return videoId;
    }
}
